package com.itheima.health.controller;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: ZPENG
 * @Date: 2020/12/4 12:40
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @GetMapping("/getLoginUsername")
    public Result getLoginUsername(){
        //获取登陆用户的认证信息
        User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //登陆用户名
        String username = user.getUsername();
        //返回给前端
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
    }
}
